public class UniquePathsWithObstacles {

    public int uniquePathsWithObstacles(int[][] obstacleGrid) {
        int m = obstacleGrid.length;
        int n = obstacleGrid[0].length;
        if (obstacleGrid[m - 1][n - 1] == 1) return 0;
        int[][] dp = new int[m][n];
        dp[m - 1][n - 1] = 1;
        for (int i = m - 1; i >= 0; i--) {
            for (int j = n - 1; j >= 0; j--) {
                if (obstacleGrid[i][j] == 1) {
                    continue;
                } else if (dp[i][j] != 1) {
                    int a = 0;
                    int b = 0;
                    if (i + 1 < m) a = dp[i + 1][j];
                    if (j + 1 < n) b = dp[i][j + 1];
                    dp[i][j] = a + b;
                }
            }
        }
        return dp[0][0];
    }
}
